<template>
  <div class="login">
    <div class="login-body">
      <div class="hread">
        <img src="../assets/logo.png" />
      </div>
      <!-- form上可以指定表单内label的宽度 -->
      <!-- 表单要进行绑定一个数据对象，item里面调用这个对象对应的属性 -->
      <!-- 绑定rules添加表单规则对象，每个属性为对象数组，每个规则为一个对象 -->
      <!-- ref引用了表单的实例对象 ，相当于将表单的实例对象发出（在该文件的this的$refs中显示）-->
      <el-form :model="form" :rules="rules" ref="loginFromRef" class="login-form" size="mini">
        <!-- item可以指定label显示左边内容 -->
        <!-- 用prop进行校验 ,需要用在item上-->
        <!-- 表单验证的名要和input数据绑定的字段相同 -->
        <el-form-item prop="username">
          <el-input placeholder="账号" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="密码" v-model="form.password" type="password"></el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input placeholder="邮箱" v-model="form.email" type="email"></el-input>
        </el-form-item>
        <el-form-item prop="mobile">
          <el-input placeholder="手机号码" v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item class="bt">
          <!-- :loading="true"为true的时候不能点击 -->
          <el-button size="small" round v-on:click="back">返回</el-button>
          <el-button type="primary" size="small" round="round" @click="regUser">注册</el-button>
          <el-button type="info" size="small" round="round" :loading="false" @click="resetFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  // 单文件组件里的data为方法，需要return数据对象
  data() {
    //   自定义表单手机号码验证
    //自定义的校验规则可以作为变量写在data中：
    var checkPhone = (rule, value, callback) => {
      //console.log(rule + value);
      if (!value) {
        return callback(new Error("手机号不能为空"));
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
        console.log(reg.test(value));
        if (reg.test(value)) {
            // 通过必须回调
          callback();
        } else {
          return callback(new Error("请输入正确的手机号"));
        }
      }
    };

    return {
      isLogin: false,
      // 定义一个表单数据绑定对象
      form: {
        username: "",
        password: "",
        mobile: "",
        email: ""
      },
      rules: {
        username: [
          //   required是否开启为空校验，trigger:"bulr"在鼠标移开的时候校验
          {
            required: true,
            message: "账号不能为空",
            trigger: ["blur", "change"]
          },
          {
            min: 3,
            max: 11,
            message: "长度最少为8，最大为11",
            trigger: ["blur", "change"]
          }
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: ["blur", "change"]
          }
        ],
        email: [
          {
            required: true,
            message: "邮箱不能为空",
            trigger: ["blur", "change"]
          },
          {
            type: "email",
            message: "请输入正确的邮箱",
            trigger: ["blur", "change"]
          }
        ],
        mobile: [
          {
            validator: checkPhone,
            trigger: ["blur", "change"]
          }
        ]
      }
    };
  },
  methods: {
    //   点击重置按钮
    resetFrom: function() {
      // console.log(this)
      //通过调用表单的resetFields方法重置
      //有默认值重置的最后为默认值
      var reset = this.$refs.loginFromRef.resetFields();
    },
    back() {
      this.$router.push("/Login");
    },
    regUser() {
           /*  | username | 用户名称 | 不能为空 |
            | password | 用户密码 | 不能为空 |
            | email    | 邮箱     | 可以为空 |
            | mobile   | 手机号   | 可以为空 |*/
      // 注册用户
      this.$refs.loginFromRef.validate((val, obj) => {
        if (!val) return;
        //调用接口注册
        this.$axios.post('users',this.form)
        .then((res)=>{
            console.log(res)
        })
     
      });
    }
  },
  computed: {}
};
</script>
// scoped样式在该组件显示
<style lang="less" scoped>
.login {
  position: absolute;
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100%;
  //  #06b9fb   ace0f9
  background: #06b9fb;
}
.login-body {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 450px;
  height: 350px;
  background: #ffffff;
  transform: translate(-50%, -50%);
  border-radius: 20px;
}
.hread {
  position: absolute;
  top: 0px;
  left: 50%;
  padding: 10px;
  width: 130px;
  height: 130px;
  border: 1px solid #eeeeee;
  box-shadow: 0 0 10px #dddddd;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  background-color: #ffffff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eeeeee;
  }
}
.login-form {
  position: absolute;
  bottom: 0px;
  width: 100%;
  padding-left: 20px;
  padding-right: 20px;
  box-sizing: border-box;
}
.bt {
  display: flex;
  justify-content: center;
}
</style>